/*
 * @(#) AttendanceAction.java 2011-9-17
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.attendance.presentation.action;

import java.util.ArrayList;
import java.util.List;

import cn.strong.gweva.attendance.business.entity.AttendanceRecordEntity;
import cn.strong.gweva.attendance.business.entity.AttendanceRecordSearchEntity;
import cn.strong.gweva.attendance.business.service.AttendanceService;
import cn.strong.gweva.common.business.entity.ComboBoxEntity;
import cn.strong.gweva.common.business.entity.PagerEntity;
import cn.strong.gweva.common.execption.ServiceException;
import cn.strong.gweva.common.presentation.action.AjaxBaseAction;
import cn.strong.gweva.common.util.ComboBoxUtil;
import cn.strong.gweva.common.util.DateUtil;

/**
 * 日常考勤Action。
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-9-17
 */
public class AttendanceAction extends AjaxBaseAction
{

	/**  */
	private static final long serialVersionUID = 6923998177531114397L;

	/** 日常考勤业务处理接口 */
	private AttendanceService attendanceService;
	
	/** 考勤记录 */
	private AttendanceRecordEntity attendanceRecord;
	
	/** 出勤状态下拉列表 */
	private List<ComboBoxEntity> attendanceStatusList;
	
	/** 检索用户考勤记录信息 */
	private AttendanceRecordSearchEntity searchEntity;

	/** 检索结果（分页） */
	private PagerEntity pagerEntity;
	
	/**
	 * 用户列表页面
	 * @return
	 */
	public String list()
	{
		try
		{
			this.setSearchEntity(new AttendanceRecordSearchEntity());
			this.getSearchEntity().setCheckDateEnd(DateUtil.getSysDate());

			// 列表页面初始化共通
			//this.listCommon(true);

			this.searchCommon(true);

			return LIST;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 用户新增页面
	 * @return
	 */
	public String add()
	{
		try
		{
			// 考勤记录
			this.setAttendanceRecord(new AttendanceRecordEntity());
			this.getAttendanceRecord().setCheckDate(DateUtil.getSysDate());
			
			// 出勤状态下拉列表
			this.setAttendanceStatusList(ComboBoxUtil.searchAttendanceStatusList());

			return ADD;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 用户修改页面
	 * @return
	 */
	public String modify()
	{
		try
		{
			this.setAttendanceRecord(this.getAttendanceService().searchAttendanceRecordById(
					this.getAttendanceRecord().getAttendanceId()));
			if (this.getAttendanceRecord() == null)
			{
				throw new ServiceException("此考勤记录不存在！");
			}
			else
			{
				// 出勤状态下拉列表
				this.setAttendanceStatusList(ComboBoxUtil.searchAttendanceStatusList());
			}

			return MODIFY;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 检索
	 * @return
	 */
	public String search()
	{
		try
		{
			this.searchCommon(false);

			return LIST;
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 校验考勤记录的唯一性。
	 * @return
	 */
	public String checkUniqueness()
	{
		try
		{
			writeJson(this.getAttendanceService().checkUniqueness(this.getAttendanceRecord()));
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return ADD;
	}
	
	/**
	 * 新增考勤记录
	 * @return
	 */
	public String insert()
	{
		try
		{
			this.getAttendanceService().insert(this.getAttendanceRecord(), this.getLoginUser());
			this.setIsSuccess("保存成功！");
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return add();
	}
	
	/**
	 * 新增考勤记录
	 * @return
	 */
	public String delete()
	{
		try
		{
			this.getAttendanceService().deleteById(this.getAttendanceRecord().getAttendanceId());
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return search();
	}
	
	/**
	 * 更新
	 * @return
	 */
	public String update()
	{
		try
		{
			this.getAttendanceService().update(this.getAttendanceRecord(), this.getLoginUser());

			return search();
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}
	
	/**
	 * 列表页面初始化共通
	 */
	private void listCommon(boolean isInit) throws Exception
	{
		// 出勤状态下拉列表
		this.setAttendanceStatusList(ComboBoxUtil.searchAttendanceStatusList());
		
		if (isInit && this.getLoginUser().getHasBindWorker())
		{
			this.getSearchEntity().setWorkerCode(this.getLoginUser().getWorkerCode());
			this.getSearchEntity().setWorkerName(this.getLoginUser().getWorkerName());
		}
	}
	
	/**
	 * 检索页面初始化共通
	 */
	private void searchCommon(boolean isInit) throws Exception
	{
		this.pagerEntity = this.getAttendanceService().searchAttendanceRecordPage(this.pagerEntity, this.getSearchEntity());
		
		// 列表页面初始化共通
		this.listCommon(isInit);
	}

	/**
	 * 获取日常考勤业务处理接口
	 * @return attendanceService 日常考勤业务处理接口
	 */
	public AttendanceService getAttendanceService()
	{
		return attendanceService;
	}
	/**
	 * 设定日常考勤业务处理接口
	 * @param attendanceService 日常考勤业务处理接口
	 */
	public void setAttendanceService(AttendanceService attendanceService)
	{
		this.attendanceService = attendanceService;
	}

	/**
	 * 获取考勤记录
	 * @return attendanceRecord 考勤记录
	 */
	public AttendanceRecordEntity getAttendanceRecord()
	{
		return attendanceRecord;
	}
	/**
	 * 设定考勤记录
	 * @param attendanceRecord 考勤记录
	 */
	public void setAttendanceRecord(AttendanceRecordEntity attendanceRecord)
	{
		this.attendanceRecord = attendanceRecord;
	}

	/**
	 * 获取出勤状态下拉列表
	 * @return attendanceStatusList 出勤状态下拉列表
	 */
	public List<ComboBoxEntity> getAttendanceStatusList()
	{
		if (this.attendanceStatusList == null)
		{
			this.attendanceStatusList = new ArrayList<ComboBoxEntity>();
		}
		return attendanceStatusList;
	}
	/**
	 * 设定出勤状态下拉列表
	 * @param attendanceStatusList 出勤状态下拉列表
	 */
	public void setAttendanceStatusList(List<ComboBoxEntity> attendanceStatusList)
	{
		this.attendanceStatusList = attendanceStatusList;
	}

	/**
	 * 获取检索用户考勤记录信息
	 * @return searchEntity 检索用户考勤记录信息
	 */
	public AttendanceRecordSearchEntity getSearchEntity()
	{
		return searchEntity;
	}

	/**
	 * 设定检索用户考勤记录信息
	 * @param searchEntity 检索用户考勤记录信息
	 */
	public void setSearchEntity(AttendanceRecordSearchEntity searchEntity)
	{
		this.searchEntity = searchEntity;
	}

	/**
	 * 获取检索结果（分页）
	 * @return pagerEntity 检索结果（分页）
	 */
	public PagerEntity getPagerEntity()
	{
		return pagerEntity;
	}
	/**
	 * 设定检索结果（分页）
	 * @param pagerEntity 检索结果（分页）
	 */
	public void setPagerEntity(PagerEntity pagerEntity)
	{
		this.pagerEntity = pagerEntity;
	}
	
}
